@echo off
::打开延迟环境变量扩展开关，否则for循环设置环境变量时不会生效，变量读取时使用!var!
setlocal ENABLEDELAYEDEXPANSION
echo vs_home=%VS2015_HOME%
set devenv="%VS2015_HOME%\Common7\IDE\devenv.com"
set msbuild="C:\Program Files (x86)\MSBuild\14.0\Bin\msbuild.exe"
set vcvars="%VS2015_HOME%\VC\vcvarsall.bat"
set sed="C:\Program Files\Git\usr\bin\sed.exe"
set tee="C:\Program Files\Git\usr\bin\tee.exe"
set ftp_dir=\\192.168.8.251\ftp\output
set ftp_url=ftp://192.168.8.251/output
::niuniu目录
set nn_path="C:\lebo\workspace\Niuniu"
set cur_path=%cd%
set datevar=%date:~0,4%%date:~5,2%%date:~8,2%
:: build type, debug or release
set type_tmp=%1
::build platform, x86 or x64
set platform=%2
::build branch
set b_branch=%3
::app or sdk
set proj=%4
set sdk_demo=%5
::适配tag打包，如果同时输入了tag和分支，优先以tag为准
set b_tag=%6


::%sed% -i s/1234/8979/g test.txt
::有时候构建时间在10点之前会出错,时获取两个字段会出现空格,导致传包失败,所以检查"时"如果是10点之前,就加0
::set mytime=%time:~0,2%%time:~3,2%
set min=%time:~3,2%
Set hour=%time:~0,2%
if %hour% leq 9 ( set hour=0%hour:~1,1%)
set mytime=%hour%%min%
set result_dir=%datevar%%mytime%

echo jenkins var as follow:
echo build_num=%BUILD_NUMBER%
echo build_user_id=%BUILD_USER_ID%
echo build_user=%BUILD_USER%
echo job_name=%JOB_NAME%
set trigger_user=%BUILD_USER_ID%
if "%BUILD_USER_ID%" == "" ( set trigger_user=%BUILD_USER% )
if "%BUILD_USER%" == ""  ( set  trigger_user=Timer)
echo user %trigger_user% trigger this build

set sdk_demo_flag=0

if "%proj%" == "app" (
    set sln_file=%cur_path%\ToB-Win-App.sln
    set ftp_dir=%ftp_dir%\ToB-Win-App
    set ftp_url=%ftp_url%/ToB-Win-App
    ::copy  /Y C:\lebo\build_bat\bak\*.h  %cur_path%\win-rsdk\include\
    more win-rsdk\include\iprierrcode.h > win-rsdk\include\iprierrcode_new.h
    more win-rsdk\include\ircast.h  > win-rsdk\include\ircast_new.h
    del win-rsdk\include\iprierrcode.h win-rsdk\include\ircast.h
    ren win-rsdk\include\iprierrcode_new.h iprierrcode.h
    ren win-rsdk\include\ircast_new.h  ircast.h
    ::app只编译pccast2
    set command_part=/t:PCCast2:Rebuild
)

if "%proj%" == "sdk" (
    set sln_file=%cur_path%\build\win32\lelinksdk.sln
    set ftp_dir=%ftp_dir%\ToB-Win-Sdk
    set ftp_url=%ftp_url%/ToB-Win-Sdk
    set command_part=/t:Rebuild
    if "%sdk_demo%" == "1" (
    set sdk_demo_flag=1
    )
)

if "%proj%" == "ppwin" (
    set sln_file=%cur_path%\windows\build\win32\ppwinsdk.sln
    set ftp_dir=%ftp_dir%\ToB-PP-Win
    set ftp_url=%ftp_url%/ToB-PP-Win
    set command_part=/t:Rebuild
)

if "%proj%" == "pplinux" (
    set sln_file=%cur_path%\linux\build\win32\pplinuxsdk.sln
    set ftp_dir=%ftp_dir%\ToB-PP-Linux
    set ftp_url=%ftp_url%/ToB-PP-Linux
    set command_part=/t:Rebuild
)

if "%proj%" == "rsdk" (
    set sln_file=%cur_path%\build\win32\lelinkrsdk.sln
    set ftp_dir=%ftp_dir%\ToB-RSDK
    set ftp_url=%ftp_url%/ToB-RSDK
    set command_part=/t:Rebuild
)

::既不是app也不是sdk,则异常推出
if not "%proj%" == "app" (
    if not "%proj%" == "sdk" (
        if not "%proj%" == "ppwin" (
            if not "%proj%" == "pplinux" (
                if not "%proj%" == "rsdk" (
                    echo "%proj% is not sdk ,app,ppwin and so exit"
                    exit 1
                )            
            )
        )
    )
)


echo "++++++++++++input config:type=%type_tmp%,platform=%platform%,proj=%proj%,b_branch=%b_branch%,b_tag=%b_tag%,sdk_demo_flag=%sdk_demo_flag%+++++"
::如果没有输入tag参数，切换到对应分支，如果输入了tag参数，那么切换到对应tag
if "%b_tag%" == "" (
    for /F %%s in ( 'git branch -a ^| findstr %b_branch%' ) do ( set branch_flag=%%s )
    echo branch_flag=!branch_flag!
    ::如果这个branch存在,则切换到branch,如果不存在,提示branch错误,显示已有的branch,然后异常退出
    if not "!branch_flag!" == "" (
        echo "checkouting %b_branch% ....."
        git checkout %b_branch%
        git pull
        echo "check branch:"
        git branch
        set b_branch=%b_branch%
    )else (
        echo "your input branch is not exist and exit  ,all branch as follow:"
        git branch -a
        exit 1
    )
)else (
    echo tag is not null
    ::特殊字符前要加转义符号^
    for /F %%j in ( 'git tag ^| findstr %b_tag%' ) do ( set tag_flag=%%j )
    echo tag_flag=!tag_flag!
    ::如果这个tag存在，则切换到tag,如果不存在，提示tag错误，并显示已有tag,然后异常退出
    if not "!tag_flag!" == "" (
        echo "checkouting %b_tag% ....."
        git checkout %b_tag%
        set b_branch=%b_tag%
    )else (
        echo "your input tag is not exist and exit  ,all tag as follow:"
        git tag
        exit 1
    )
)

for /F %%i in ('git rev-parse --short HEAD') do ( set commitid=%%i)
echo commitid=%commitid%
echo "=====================set %platform% env========================"
if "%platform%"=="x86" call %vcvars% x86
if "%platform%"=="x64"  call %vcvars% x86_amd64
::编译app时，需要依赖sdk，并修改工程文件满足vs2015
if "%proj%" == "app" (
    echo "===============call CopyRelease==============================="
    call  CopyRelease.bat
)
set result_dir=%result_dir%_%b_branch%_%commitid%_%trigger_user%_%platform%_%type_tmp%
if "%type_tmp%" == "Release_Static" (
    echo "++++++++++++++++++++++it is Release Static+++++++++++++++++++"
    set type="Release Static"
)else if "%type_tmp%" == "Debug_Static" (
    echo "++++++++++++++++++++++it is Debug Static+++++++++++++++++++++++"
    set type="Debug Static"
)else (
    echo "++++++++++++++++++++++++change type+++++++++++++++++++++++++++++"
    set type=%type_tmp%
)

git status
echo "=============start building:%type%,%platform%,%proj%,%b_branch%================="
echo now path=%cd%

echo "build command:%msbuild% %sln_file% %command_part%  /p:Configuration=%type%;Platform=%platform% | %tee% build_log.txt"
%msbuild% %sln_file% %command_part% /p:Configuration=%type%;Platform=%platform% | %tee% build_log.txt

if "%sdk_demo_flag%" == "1" (
    echo ===============================sdk demo build===================================
    %msbuild% %cur_path%\test\SDKDemo\SDKDemo.sln /t:SDKDemo:Rebuild /p:Configuration=%type%;Platform=%platform% | %tee% build_log.txt
    echo "===================copy result to ftp  %result_dir%_demo_sdk  =========================="
    echo "xcopy /S /E  /Y %cur_path%\test\SDKDemo\SDK %ftp_dir%\%result_dir%_demo_sdk\"
    xcopy /S /E  /Y C:\lebo\workspace\ToB-Win-Sdk\test\SDKDemo\SDK %ftp_dir%\%result_dir%_demo_sdk\
    echo ftp_path=%ftp_url%/%result_dir%_demo_sdk/
    echo jenkins ftp_path=%ftp_url%/%result_dir%_demo_sdk/ > result_ftp_path.txt 
    exit 0
)

::devenv 的 /projectconfig 必须和/project 参数一起使用
::%devenv% /Rebuild %type% %proj%.sln  /project "%proj%/%proj%.vcxproj" /projectconfig "%type%|%platform%"
REM echo "build finish and result as follow:"
REM more build_log.txt | findstr "个错误"  > build_result.txt
REM more build_result.txt


echo "===================copy result to ftp  %result_dir%  =========================="
if "%proj%" == "sdk" (
    if not exist bin\win32\%type%\lbmain.dll (
        echo not exist lbmain.dll and exit
        exit 1
    )
    md target\bin
    md target\api
    md target\lib
    md target\mdns
    copy bin\win32\%type%\*.dll target\bin\
    copy bin\win32\%type%\*.pdb target\bin\
    copy framecore\poco\bin\*.dll target\bin\
    copy api\*  target\api\
    copy lib\win32\%type%\* target\lib\
    copy test\lbmdnstest\bin\* target\mdns
    more include\lb_version.h |findstr "LB_SDK_VERSION_MAIN"  >sdk_version
    echo "*******************copy sdk version *********************"
    copy sdk_version C:\lebo\build_bat\tools\
    echo "*******************copy sdk path for exe *********************"
    echo sdk_path=%ftp_url%/%result_dir%/ > sdk_path_info
    copy sdk_path_info C:\lebo\build_bat\tools\
)

if "%proj%" == "app" (
    if not exist Bin\PCCast2\Update*.dll (
        echo not exist PCCast.exe and exit
        exit 1
    )

    if not exist Bin\PCCast2\PCCast*.exe (
        echo not exist PCCast.exe and exit
        exit 1
    )
    md target
    del /S /Q %nn_path%\PCCast\*
    cd Installer
    md PCCast
    call Init-PCCast.bat
    call build-PCCast.bat
    cd %cur_path%
    echo "**********************change LeboPCCast name ******************"
    copy C:\lebo\build_bat\tools\sdk_version .\
    for /f "delims="  %%m in ( sdk_version ) do ( set version_tmp=%%m )
    echo last=!version_tmp!
    ::截取倒数第二个到倒数第九个字符之间的字符，就是对应的版本号
    set sdk_v=!version_tmp:~-9,-2!
    echo version=!sdk_v!
    move Installer\LeboPCCast.exe   target\LeboPCCast_!sdk_v!.exe
    ::copy Bin\Receiver\*.exe target\
    ::copy Bin\Sender\HPCastSender*.exe target\
    copy C:\lebo\build_bat\tools\sdk_path_info .\target\
    echo target files as follow:
    dir target\
)
if "%proj%" == "ppwin" (
    echo this ppwin build done
    if not exist windows\bin\win32\%type%\lbmain.dll (
        echo not exist lbmain.dll and exit
        exit 1
    )
    md target\bin
    md target\api
    md target\lib
    copy windows\bin\win32\%type%\*.dll target\bin\
    copy windows\bin\win32\%type%\*.pdb target\bin\
    copy windows\framecore\poco\win32\bin\*.dll target\bin\
    copy windows\api\*  target\api\
    copy windows\lib\win32\%type%\* target\lib\
)

if "%proj%" == "pplinux" (
    echo this pplinux build done
    exit 0
)

if "%proj%" == "rsdk" (
    echo this ToB-RSDK windows build done  
    exit 0
)
::@echo on
echo "xcopy /S /E  /Y %cur_path%\target %ftp_dir%\%result_dir%\"
xcopy /S /E  /Y %cur_path%\target %ftp_dir%\%result_dir%\
echo ftp_path=%ftp_url%/%result_dir%/
echo jenkins ftp_path=%ftp_url%/%result_dir%/ > result_ftp_path.txt
:: ping 127.0.0.1 -n 6 > null  (bat 中sleep的表达)
::卸载时的版本号写在C:\lebo\workspace\Niuniu\SetupScripts\PCCast\PCCast.nsi